home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / OPTANS10.ARJ / DEMOANSI.C < prev    next >
C/C++ Source or Header  |  1992-06-14  |  13KB  |  460 lines

  1. #include "optansi.h"
  2. #include "auser.h"
  3. #include "skeys.h"
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9.  
  10. /* Global variables */
  11. // Counters and Timers
  12. unsigned ocnt, nocnt;
  13. clock_t begin, otime, notime;
  14.  
  15.  
  16.  
  17. pl(char *s) {
  18.     printf(s);
  19.     aupdatexy();
  20.     if(getch()==27)
  21.         exit(1);
  22.     return;
  23. }
  24.  
  25.  
  26. int test1() {
  27. #define MEMBERS 10
  28. // X positions
  29. const char x[]={ 1,  2, 78,  78,  1,  1, 39, 39,  1,  1 };
  30. // Y positions
  31. const char y[]={ 1,  2,  2,  24, 24,  2, 12, 13, 14,  1 };
  32. // Attributes
  33. const char a[]={ 2,  7,  15, 31, 14, 13, 63,191, 79,  7 };
  34. int i, j;
  35.     // Optimizations Count
  36.     acolor(7);
  37.     aclrscr();
  38.     ocnt=0;
  39.     otime=0;
  40.     for (j=0; j<100; j++) {
  41.         for (i=0; i<MEMBERS; i++) {
  42.             begin=clock();
  43.             ocnt+=agotoxy(x[i], y[i]);        // Move cursor to new position
  44.             ocnt+=acolor(a[i]);                        // Set new color
  45.             printf("%X\b", i);                        // Put hex number
  46.             ocnt++;                                                // Add 1 for output of hex character
  47.             otime+=clock()-begin;
  48.             if (!j) {
  49.                 gotoxy(65, 1);                // conio functions used to avoid interfering with ANSI
  50.                 printf("X%d Y%d A%d %X", x[i], y[i], a[i], i);
  51.                 aclreol();
  52.                 if (getch()==27)
  53.                     exit(1);
  54.                 if (i==MEMBERS-1)    {
  55.                     gotoxy(63, 1);
  56.                     atextattr(7);
  57.                     printf("Now 100x repeated");
  58.                     if (getch()==27)
  59.                         exit(1);
  60.                 }
  61.                 gotoxy(x[i], y[i]);        // Restore location
  62.             }
  63.         }
  64.         begin=clock();
  65.         ocnt+=acolor(7);                                // Set color for clear screen to black
  66.         ocnt+=aclrscr();                                // Clear the screen
  67.         otime+=clock()-begin;
  68.     }
  69.  
  70.     // Non-optimizations Count
  71.     atextattr(7);
  72.     aclrscr();
  73.     gotoxy(63, 1);
  74.     pl("Now Nonoptimized");
  75.     nocnt=0;
  76.     notime=0;
  77.     for (j=0; j<100; j++) {
  78.         for (i=0; i<MEMBERS; i++) {
  79.             begin=clock();
  80.             nocnt+=oldgotoxy(x[i], y[i]);    // Move cursor to new position
  81.             nocnt+=oldcolor(a[i]);                // Set new color
  82.             printf("%X\b", i);                        // Put hex number
  83.             nocnt++;                                            // Add 1 for output of hex character
  84.             notime+=clock()-begin;
  85.         }
  86.         begin=clock();
  87.         nocnt+=oldcolor(7);                            // Set color for clear screen to black
  88.         nocnt+=oldclrscr();                            // Clear the screen
  89.         notime+=clock()-begin;
  90.     }
  91.     return;
  92. }
  93.  
  94.  
  95. int test2() {
  96.     int i, j;
  97.     struct point {
  98.         char x, y, a;
  99.     } *ptr, *st;
  100.  
  101.     if ((st = malloc(100*MEMBERS*sizeof(struct point))) == NULL)
  102.     {
  103.          printf("Not enough memory for Test 2\n");
  104.          return(0);
  105.     }
  106.     ptr=st;
  107.  
  108.  
  109.     // Optimizations Count
  110.     randomize();
  111.     acolor(7);
  112.     aclrscr();
  113.     ocnt=0;
  114.     otime=0;
  115.     for (j=0; j<100*MEMBERS; j++) {
  116.         ptr->x=random(80)+1;
  117.         ptr->y=random(24)+1;
  118.         ptr->a=random(256);
  119.  
  120.         begin=clock();
  121.         ocnt+=agotoxy(ptr->x, ptr->y);        // Move cursor to new position
  122.         ocnt+=acolor(ptr->a);                            // Set new color
  123.         ocnt+=printf("%X", j);                        // Put hex number
  124.         aupdatexy();
  125.         otime+=clock()-begin;
  126.  
  127.         ptr++;
  128.     }
  129.  
  130.     ptr=st;
  131.     // Non-optimizations Count
  132.     gotoxy(63, 1);
  133.     atextattr(7);
  134.     pl("Now Nonoptimized");
  135.     aclrscr();
  136.     nocnt=0;
  137.     notime=0;
  138.     for (j=0; j<100*MEMBERS; j++) {
  139.         begin=clock();
  140.         nocnt+=oldgotoxy(ptr->x, ptr->y);    // Move cursor to new position
  141.         nocnt+=oldcolor(ptr->a);                    // Set new color
  142.         nocnt+=printf("%X", j);                        // Put hex number
  143.         notime+=clock()-begin;
  144.  
  145.         ptr++;
  146.     }
  147.     free(st);
  148.     return(1);
  149. }
  150.  
  151.  
  152. test3() {
  153.     char s[25];
  154.     unsigned i;
  155.  
  156.     /* LOCATION FUNCTIONS */
  157.     aclrscr();
  158.     pl("LOCATION FUNCTIONS:  aclrscr()\n");
  159.     pl("aupdatexy() - updates axpos and aypos after a printf\n");
  160.     aupdatexy();
  161.     pl("aupdatexy1() - same as aupdatexy except that it uses low memory to get values\n");
  162.     aupdatexy1();
  163.     ahome();            /* MAJOR BUG HERE */
  164.     pl("ahome()");
  165.     aclreol();
  166.     pl("");
  167.     pl(" aclreol() - Note how everything disappeared to the right?");
  168.     adown(5);
  169.     pl("adown(5)");
  170.     aleft(25);
  171.     pl("aleft(25)");
  172.     aup(2);
  173.     pl("aup(2)");
  174.     aright(7);
  175.     pl("aright(7)");
  176.     agotoxy(1,12);
  177.     pl("agotoxy(1,12)");
  178.     pl("\nasavecursor() - Note the cursor location now ->");
  179.     asavecursor();
  180.     // Note this next line uses oldgotoxy which does not update the global variables axpos and aypos
  181.     oldgotoxy(1,24);
  182.     pl("gotoxy(1,24) - This goto uses some other goto function that doesn't update axpos  and aypos so aforcegotoxy can help to resolve");
  183.     aforcegotoxy(1,12);
  184.     pl("aforcegotoxy(1,12)");
  185.     arestorecursor();
  186.     pl("X\narestorecursor() - location is restored to where the X above is placed");
  187.  
  188.     /* COLOR FUNCTIONS */
  189.     aclrscr();
  190.     pl("COLOR FUNCTIONS:\n");
  191.     acolor(ATTR(YELLOW, GREEN));
  192.     pl("acolor(ATTR(YELLOW,GREEN))\n");
  193.     atextattr(ATTR(WHITE,BLUE));
  194.     pl("atextattr(ATTR(WHITE,BLUE))  - same as acolor\n");
  195.     atextcolor(LIGHTRED);
  196.     pl("atextcolor(LIGHTRED) - foreground color\n");
  197.     atextbackground(RED);
  198.     pl("atextbackground(RED) - background color\n");
  199.     aresetcolor();
  200.     pl("aresetcolor()\n");
  201.     ahighlight();
  202.     pl("ahighlight() or ahighvideo()\n");
  203.     ablink();
  204.     pl("ablink()\n");
  205.     anohighlight();
  206.     pl("anohighlight() or alowvideo()\n");
  207.     //    anormvideo()
  208.     anoblink();
  209.     pl("anoblink()\n\nThese next 3 functions are special ANSI color commands and behave differently\n  on different emulators. See DOCS.\n");
  210.     aunderline(;);
  211.     pl("aunderline(equation)   ");
  212.     areverse(;);
  213.     pl("areverse(equation)   ");
  214.     ainvisible(;);
  215.     pl("ainvisible(equation)\n");
  216.     oldcolor(7);
  217.     printf("That last color was ainvisible() in case you could not see it.\n\n");
  218.     pl("The color has been changed using a function that doesn't update acurattr,\n  so aforcecolor can be used to resolve\n");
  219.     aforcecolor(ATTR(WHITE,CYAN));
  220.     pl("aforcecolor(ATTR(WHITE,CYAN))\n");
  221.  
  222.     /* MISCELLANEOUS FUNCTIONS */
  223.     acolor(7);
  224.     aclrscr();
  225.     pl("MISCELLANEOUS FUNCTIONS:\n");
  226.  
  227.     printf(
  228.         "Multiple test methods that test for ANSI presence -\n"
  229.         "atest()\n"
  230.         "  aresult():  "
  231.     );
  232.     atest();
  233.     for (i=0;kbhit();i++)        // read input buffer
  234.         s[i]=getch();
  235.     if (aresult(s))
  236.         printf("TRUE\n");
  237.     else
  238.         printf("FALSE\n");
  239.  
  240.     printf(
  241.         "atest1()\n"
  242.         "  aresult1(): "
  243.     );
  244.     atest1();
  245.     if (aresult1())
  246.         printf("TRUE\n");
  247.     else
  248.         printf("FALSE\n");
  249.  
  250.     printf("aresult2():   ");
  251.     if (aresult2())
  252.         printf("TRUE\n");
  253.     else
  254.         printf("FALSE\n");
  255.  
  256.     atest();
  257.     for (i=0;kbhit();i++)        // read input buffer
  258.         s[i]=getch();
  259.     i=awherexy(s);
  260.     printf("awherexy():   Cursor at X:%d Y:%d at beginning of this line\n\n", i/256, i%256);
  261.  
  262.     pl("akeyboard() will allow you to redefine keyboard or create macros if your ANSI\n  emulator supports them.\n");
  263.     pl("asetmode() and aresetmode() changes screen modes and variable options. Watch.\n\nasetmode(CO40)");
  264.     asetmode(C40);
  265.     pl("now back to asetmode(C80)");
  266.     asetmode(C80);
  267.     pl("\n\n\n\nanocursorwrap() - turns off cursor wrap\n");
  268.     anocursorwrap();
  269.     // These printf strings exceed 80 columns.
  270.     printf("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
  271.     pl("acursorwrap() - turns it back on.. Below is the same string as it should have\n  appeared.\n");
  272.     acursorwrap();
  273.     printf("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
  274.  
  275.     pl("That concludes DEMOANSI tests.. ");
  276.  
  277.     return;
  278. }
  279.  
  280.  
  281. results(int testnum) {
  282.  
  283.     aforcegotoxy(57, 1);
  284.     aforcecolor(7);
  285.     pl("Press a key for results");
  286.  
  287.     aclrscr();
  288.     printf("TEST %d\n", testnum);
  289.     printf("\nOPTIMIZED ANSI STATISTICS (OPTANSI)\n");
  290.     printf(
  291.         "Total number of characters counted in 100 repetitions of ANSI output: %5d\n"
  292.         "At 2400 baud that would equate to %2.2f seconds.\n"
  293.         "At 9600 baud that would equate to %2.2f seconds.\n"
  294.         "Time of operations on this computer was %2.4f seconds.\n",
  295.         ocnt, ocnt/240.0, ocnt/960.0, (float) otime/CLK_TCK);
  296.     printf("\nNON-OPTIMIZED ANSI STATISTICS (OTHER LIBRARIES)\n");
  297.     printf(
  298.         "Total number of characters counted in 100 repetitions of ANSI output: %5d\n"
  299.         "At 2400 baud that would equate to %2.2f seconds.\n"
  300.         "At 9600 baud that would equate to %2.2f seconds.\n"
  301.         "Time of operations on this computer was %2.4f seconds.\n",
  302.         nocnt, nocnt/240.0, nocnt/960.0, (float) notime/CLK_TCK);
  303.     printf("\nRELATIONAL IMPROVEMENT WHEN USING OPTANSI\n");
  304.     printf(
  305.         "Percent of output reduced using OptANSI library: %2.2f%%\n"
  306.         "At 2400 baud that would reduce time by %2.2f seconds.\n"
  307.         "At 9600 baud that would reduce time by %2.2f seconds.\n"
  308.         "Time saved on this computer was %2.4f seconds, %2.2f%%.\n",
  309.         (float)(nocnt-ocnt)/nocnt*100, (nocnt-ocnt)/240.0, (nocnt-ocnt)/960.0,
  310.         (float) (notime-otime)/CLK_TCK, (float) (notime-otime)/notime*100);
  311.     printf(
  312.         "\n"
  313.         "\n"
  314.         "Note: The DEMO version of the library has been slightly crippled to perform less"
  315.         "efficiently than the registered version."
  316.     );
  317.     return;
  318. }
  319.  
  320.  
  321.  
  322. main() {
  323.  
  324.     ainit();
  325.     ashort(aBKSP | aCR | !aLF | !aCLRSCR | aRESET | aNANSI);
  326.     atest1();
  327.     if (!(aresult1() || aresult2())) {
  328.         printf(
  329.             "DEMOANSI does not detect an ANSI device driver. Make sure to place\n"
  330.             "DEVICE=ANSI.SYS or equivalent in your CONFIG.SYS and reboot your machine\n"
  331.             "If you would like to continue this test without ANSI, press ENTER.\n"
  332.         );
  333.         if (getch()!=13)
  334.             return;
  335.     }
  336.  
  337.     aforcecolor(31);
  338.     aclrscr();
  339.     aright(70);
  340.     printf("ESC Aborts");
  341.     agotoxy(1, 4);
  342.     printf(
  343.         "DEMOANSI: Welcome to the OptANSI library demo:\n"
  344.         "\n"
  345.         "ANSI, as you probably already know, is a useful emulation tool which adds\n"
  346.         "color, cursor location, and other functions to the otherwise standard\n"
  347.         "unidirectional TTY interface. It does this by sending a special set of\n"
  348.         "codes to the system for interpretting. ANSI has established its domain with\n"
  349.         "telecommunications and device drivers. ANSI codes tend to be very blocky and\n"
  350.         "in most circumstances are large strings for even simple commands. For instance,\n"
  351.         "to change a simple color, the ANSI string might be ESC[0;37;44;1;5m. Often\n"
  352.         "times all this data is not necessary. That's where OptANSI comes it. It takes\n"
  353.         "advantage of the fact that clock cycles on the computer are faster than the\n"
  354.         "communications interface and can find the shortest path, reducing the ANSI\n"
  355.         "string.\n"
  356.         "\n"
  357.         "Programmers should note that all the commands are highly optimized so that the\n"
  358.         "entire OptANSI code and data fits well under 1.5K. And all commands are very\n"
  359.         "similar to conio functions of the same name.\n"
  360.         "\n"
  361.     );
  362.     pl("**press a key**");
  363.  
  364.  
  365.     /* TEST 1 */
  366.     aclrscr();
  367.     adown(6);
  368.     printf(
  369.         "TEST 1\n"
  370.         "\n"
  371.         "On this next screen there will be a test of some of the most common ANSI\n"
  372.         "output codes. First the system will show you all the commands it is going to\n"
  373.         "send to the screen. Press a key after each of these. Then it will repeat these\n"
  374.         "same commands 100 times to calculate the output returned from OptANSI functions."
  375.         "Then it will repeat the same test using nonoptimized ANSI functions so you can\n"
  376.         "see the difference. You will find some bias in the results though. We have used\n"
  377.         "some of the cursor location functions that we know will optimize well. But this\n"
  378.         "is only to assure you of the extreme that OptANSI can be useful.\n"
  379.         "\n"
  380.     );
  381.     pl("**press a key**");
  382.  
  383.     test1();
  384.     results(1);
  385.     pl("");
  386.  
  387.  
  388.     /* TEST 2 */
  389.     aforcecolor(31);
  390.     aclrscr();
  391.     adown(7);
  392.     printf(
  393.         "TEST 2\n"
  394.         "\n"
  395.         "On this next screen there will be a test similar to the last test except that\n"
  396.         "instead of best case scenario output, completely random screen and color data\n"
  397.         "will be sent to the ANSI output functions. This should severely reduce the\n"
  398.         "results OptANSI offers as compared with the last performance test, but you\n"
  399.         "should notice that OptANSI still offers significant improvements. Running this\n"
  400.         "second test will give varied results each time but only to a limited degree.\n"
  401.         "\n"
  402.     );
  403.     pl("**press a key**");
  404.  
  405.     if (test2()) {
  406.         results(2);
  407.         getch();
  408.     }
  409.  
  410.  
  411.     /* TEST 3 */
  412.     aforcecolor(31);
  413.     aclrscr();
  414.     adown(8);
  415.     printf(
  416.         "TEST 3\n"
  417.         "\n"
  418.         "This last test is designed to test the majority of functions available in the\n"
  419.         "OptANSI library. It should give you a feel for the easiness of its use and\n"
  420.         "ensure that they work properly. After each command, press a key. Pay careful\n"
  421.         "attention to the current colors and current cursor locations just before\n"
  422.         "pressing a key.\n"
  423.         "\n"
  424.     );
  425.     pl("**press a key**");
  426.  
  427.     acolor(7);
  428.     test3();
  429.  
  430.     return;
  431. }
  432.  
  433.  
  434. /* These functions were ported from other standard ANSI output libraries */
  435.  
  436. int oldgotoxy(int x, int y) {
  437.     return(printf("\x1b[%d;%dH", y, x));
  438. }
  439.  
  440. EXTERN char acolors[];
  441.  
  442. int oldcolor(int c) {
  443. char str[25];
  444. int f, b;
  445.  
  446.     f=c & 7;
  447.     b=(c & 112) >> 4;
  448.     sprintf(str, "\x1b[0;3%c;4%c", acolors[f], acolors[b]);
  449.     if (c & 8)
  450.         strcat(str, ";1");
  451.     if (c & 128)
  452.         strcat(str, ";5");
  453.     strcat(str, "m");
  454.     return(printf(str));
  455. }
  456.  
  457. int oldclrscr() {
  458.     return(printf("\x1b[2J"));
  459. }
  460.